<?php
namespace Wap\Controller;
 
class ProductController extends CommonController {
	/*
	* 获取分区
	*/
	private function getNavArea() {
		$shop_area = M('shop_area') -> where('shop_area_state = 1') -> order('shop_area_sort ASC') -> select() ;
		$this -> assign('area_list', $shop_area);
		return $shop_area;
	}

	/**
	 * 产品区
	 */
	public function index() {
		$shop_area = $this -> getNavArea();
		$area_id = empty(trim(I('get.area_id'))) ? $shop_area[0]['shop_area_id'] : I('get.area_id');
		$str['shop_area_ids'] = array('LIKE','%\"' . $area_id . '\"%');
		$str['goods_state'] = array('EQ', 1);
		$str['goods_verify'] = array('EQ', 1);
		$num = 6;
		if(IS_POST) {
			$peg = I('post.peg');
			$result = _getList($this -> tm_product, $str, '', null, 'id desc', $peg, $num);
			if(empty($result)) {
					echo json_encode(0);
				} else if($peg > 10) {
					echo json_encode(0);
				} else {
					echo json_encode($result);
				}
			exit;
		}
		$goods = _getList($this -> tm_product, $str, '', null, 'id desc', 1, $num);
		$this->assign('area_id', $area_id);
		$this->assign('goods', $goods);
		$this -> display();
	}

	/**
	 * 抢购区
	 */
	public function purchase() {
		$this -> display();
	}
	
	/**
	 * 产品详细
	 */
	public function detail() {
		$id = I('get.id');
		if($id) {
			$good = M($this -> tm_product) -> find($id);
			$good['goods_spec'] = unserialize($good['goods_spec']);
			$good['goods_color'] = unserialize($good['goods_color']);
			$good['goods_spec_arr'] = unserialize($good['goods_spec_arr']);
			$good['content'] = html_entity_decode($good['content']);
			$good['imgset'] = strAnalysis($good['imgset']);
			if($_SESSION['user_id']) {
				$collect = M('collect') -> where('userid = ' . $_SESSION['user_id'] . ' and sc_id = ' . $good['id'] . ' and sc_lx = "sp"') -> find();
			} else {
				$collect = '';
			}
			$this -> assign('collect', $collect);
			$this -> assign('good', $good);
			$this -> display();
		} else {
			$this -> error('非法操作');
		}
	}
}